home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / toolkits / libaux / aux.c.z / aux.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  8.1 KB  |  375 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <X11/Xlib.h>
  5. #include <X11/Xutil.h>
  6. #include <X11/keysym.h>
  7. #include <GL/glx.h>
  8. #include <GL/gl.h>
  9. #include "tk.h"
  10. #include "aux.h"
  11.  
  12. #if defined(__cplusplus) || defined(c_plusplus)
  13. #define class c_class
  14. #endif
  15.  
  16.  
  17. static struct {
  18.     int keyField;
  19.     void (*KeyFunc)(void);
  20. } keyTable[200];
  21.  
  22. static struct {
  23.     int mouseField;
  24.     void (*MouseFunc)(AUX_EVENTREC *);
  25. } mouseDownTable[20], mouseUpTable[20], mouseLocTable[20];
  26.  
  27. static int keyTableCount = 0;
  28. static int mouseDownTableCount = 0;
  29. static int mouseUpTableCount = 0;
  30. static int mouseLocTableCount = 0;
  31. static GLenum displayModeType = 0;
  32. static GLenum displayModePolicy = AUX_MINIMUM_CRITERIA;
  33. static int displayModeID = 0;
  34.  
  35.  
  36. static void DefaultHandleReshape(int w, int h)
  37. {
  38.     glViewport(0, 0, w, h);
  39.     glMatrixMode(GL_PROJECTION);
  40.     glLoadIdentity();
  41.     glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
  42.     glMatrixMode(GL_MODELVIEW);
  43.     glLoadIdentity();
  44. }
  45.  
  46. static void DefaultHandleExpose(int w, int h)
  47. {
  48. }
  49.  
  50. static GLenum MouseLoc(int x, int y, GLenum button)
  51. {
  52.     AUX_EVENTREC info;
  53.     GLenum flag;
  54.     int i;
  55.  
  56.     flag = GL_FALSE;
  57.     for (i = 0; i < mouseLocTableCount; i++) {
  58.     if ((button & AUX_LEFTBUTTON) == mouseLocTable[i].mouseField) {
  59.         info.event = AUX_MOUSELOC;
  60.         info.data[AUX_MOUSEX] = x;
  61.         info.data[AUX_MOUSEY] = y;
  62.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  63.         (*mouseLocTable[i].MouseFunc)(&info);
  64.         flag |= GL_TRUE;
  65.     }
  66.     if ((button & AUX_RIGHTBUTTON) == mouseLocTable[i].mouseField) {
  67.         info.event = AUX_MOUSELOC;
  68.         info.data[AUX_MOUSEX] = x;
  69.         info.data[AUX_MOUSEY] = y;
  70.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  71.         (*mouseLocTable[i].MouseFunc)(&info);
  72.         flag |= GL_TRUE;
  73.     }
  74.     if ((button & AUX_MIDDLEBUTTON) == mouseLocTable[i].mouseField) {
  75.         info.event = AUX_MOUSELOC;
  76.         info.data[AUX_MOUSEX] = x;
  77.         info.data[AUX_MOUSEY] = y;
  78.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  79.         (*mouseLocTable[i].MouseFunc)(&info);
  80.         flag |= GL_TRUE;
  81.     }
  82.     }
  83.     return flag;
  84. }
  85.  
  86. static GLenum MouseUp(int x, int y, GLenum button)
  87. {
  88.     AUX_EVENTREC info;
  89.     GLenum flag;
  90.     int i;
  91.  
  92.     flag = GL_FALSE;
  93.     for (i = 0; i < mouseUpTableCount; i++) {
  94.     if ((button & AUX_LEFTBUTTON) == mouseUpTable[i].mouseField) {
  95.         info.event = AUX_MOUSEUP;
  96.         info.data[AUX_MOUSEX] = x;
  97.         info.data[AUX_MOUSEY] = y;
  98.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  99.         (*mouseUpTable[i].MouseFunc)(&info);
  100.         flag |= GL_TRUE;
  101.     }
  102.     if ((button & AUX_RIGHTBUTTON) == mouseUpTable[i].mouseField) {
  103.         info.event = AUX_MOUSEUP;
  104.         info.data[AUX_MOUSEX] = x;
  105.         info.data[AUX_MOUSEY] = y;
  106.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  107.         (*mouseUpTable[i].MouseFunc)(&info);
  108.         flag |= GL_TRUE;
  109.     }
  110.     if ((button & AUX_MIDDLEBUTTON) == mouseUpTable[i].mouseField) {
  111.         info.event = AUX_MOUSEUP;
  112.         info.data[AUX_MOUSEX] = x;
  113.         info.data[AUX_MOUSEY] = y;
  114.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  115.         (*mouseUpTable[i].MouseFunc)(&info);
  116.         flag |= GL_TRUE;
  117.     }
  118.     }
  119.     return flag;
  120. }
  121.  
  122. static GLenum MouseDown(int x, int y, GLenum button)
  123. {
  124.     AUX_EVENTREC info;
  125.     GLenum flag;
  126.     int i;
  127.  
  128.     flag = GL_FALSE;
  129.     for (i = 0; i < mouseDownTableCount; i++) {
  130.     if ((button & AUX_LEFTBUTTON) == mouseDownTable[i].mouseField) {
  131.         info.event = AUX_MOUSEDOWN;
  132.         info.data[AUX_MOUSEX] = x;
  133.         info.data[AUX_MOUSEY] = y;
  134.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  135.         (*mouseDownTable[i].MouseFunc)(&info);
  136.         flag |= GL_TRUE;
  137.     }
  138.     if ((button & AUX_RIGHTBUTTON) == mouseDownTable[i].mouseField) {
  139.         info.event = AUX_MOUSEDOWN;
  140.         info.data[AUX_MOUSEX] = x;
  141.         info.data[AUX_MOUSEY] = y;
  142.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  143.         (*mouseDownTable[i].MouseFunc)(&info);
  144.         flag |= GL_TRUE;
  145.     }
  146.     if ((button & AUX_MIDDLEBUTTON) == mouseDownTable[i].mouseField) {
  147.         info.event = AUX_MOUSEDOWN;
  148.         info.data[AUX_MOUSEX] = x;
  149.         info.data[AUX_MOUSEY] = y;
  150.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  151.         (*mouseDownTable[i].MouseFunc)(&info);
  152.         flag |= GL_TRUE;
  153.     }
  154.     }
  155.     return flag;
  156. }
  157.  
  158. static GLenum KeyDown(int key, GLenum status)
  159. {
  160.     GLenum flag;
  161.     int i;
  162.  
  163.     flag = GL_FALSE;
  164.     if (keyTableCount) {
  165.     for (i = 0; i < keyTableCount; i++) {
  166.         if (key == keyTable[i].keyField) {
  167.         (*keyTable[i].KeyFunc)();
  168.         flag |= GL_TRUE;
  169.         }
  170.     }
  171.     }
  172.     return flag;
  173. }
  174.  
  175. void auxExposeFunc(void (*Func)(int, int))
  176. {
  177.     tkExposeFunc(Func);
  178. }
  179.  
  180. void auxReshapeFunc(void (*Func)(int, int))
  181. {
  182.     tkExposeFunc(Func);
  183.     tkReshapeFunc(Func);
  184. }
  185.  
  186. void auxIdleFunc(void (*Func)(void))
  187. {
  188.     tkIdleFunc(Func);
  189. }
  190.  
  191. void auxKeyFunc(int key, void (*Func)(void))
  192. {
  193.     keyTable[keyTableCount].keyField = key;
  194.     keyTable[keyTableCount++].KeyFunc = Func;
  195. }
  196.  
  197. void auxMouseFunc(int mouse, int mode, void (*Func)(AUX_EVENTREC *))
  198. {
  199.     if (mode == AUX_MOUSEDOWN) {
  200.     mouseDownTable[mouseDownTableCount].mouseField = mouse;
  201.     mouseDownTable[mouseDownTableCount++].MouseFunc = Func;
  202.     } else if (mode == AUX_MOUSEUP) {
  203.     mouseUpTable[mouseUpTableCount].mouseField = mouse;
  204.     mouseUpTable[mouseUpTableCount++].MouseFunc = Func;
  205.     } else if (mode == AUX_MOUSELOC) {
  206.     mouseLocTable[mouseLocTableCount].mouseField = mouse;
  207.     mouseLocTable[mouseLocTableCount++].MouseFunc = Func;
  208.     } 
  209. }
  210.  
  211. void auxMainLoop(void (*Func)(void))
  212. {
  213.     tkDisplayFunc(Func);
  214.     tkExec();
  215. }
  216.  
  217. void auxInitPosition(int x, int y, int width, int height)
  218. {
  219.     tkInitPosition(x, y, width, height);
  220. }
  221.  
  222. void auxInitDisplayMode(GLenum type)
  223. {
  224.     displayModeType = type;
  225.     tkInitDisplayMode(type);
  226. }
  227.  
  228. void auxInitDisplayModePolicy(GLenum type)
  229. {
  230.  
  231.     displayModePolicy = type;
  232.     tkInitDisplayModePolicy(type);
  233. }
  234.  
  235. void auxInitDisplayModeID(GLint id)
  236. {
  237.  
  238.     displayModeID = id;
  239.     tkInitDisplayModeID(id);
  240. }
  241.  
  242. GLenum auxInitWindow(char *title)
  243. {
  244.     int useDoubleAsSingle = 0;
  245.  
  246.     if (tkInitWindow(title) == GL_FALSE) {
  247.     if (AUX_WIND_IS_SINGLE(displayModeType)) {
  248.         tkInitDisplayMode(displayModeType|AUX_DOUBLE);
  249.         if (tkInitWindow(title) == GL_FALSE) {
  250.         return GL_FALSE;
  251.         }
  252.         fprintf(stderr, "Can't initialize a single buffer visual.\n");
  253.         fprintf(stderr, "Will use a double buffer visual instead,");
  254.         fprintf(stderr, "only drawing into the front buffer.\n");
  255.         displayModeType = displayModeType | AUX_DOUBLE;
  256.         useDoubleAsSingle = 1;
  257.     }
  258.     else {
  259.         return GL_FALSE;
  260.     }
  261.     }
  262.     tkReshapeFunc(DefaultHandleReshape);
  263.     tkExposeFunc(DefaultHandleExpose);
  264.     tkMouseUpFunc(MouseUp);
  265.     tkMouseDownFunc(MouseDown);
  266.     tkMouseMoveFunc(MouseLoc);
  267.     tkKeyDownFunc(KeyDown);
  268.     auxKeyFunc(AUX_ESCAPE, auxQuit);
  269.     glClearColor(0.0, 0.0, 0.0, 1.0);
  270.     glClearIndex(0);
  271.     glLoadIdentity();
  272.     if (useDoubleAsSingle) {
  273.         glReadBuffer(GL_FRONT);
  274.     glDrawBuffer(GL_FRONT);
  275.     }
  276.     return GL_TRUE;
  277. }
  278.  
  279. void auxCloseWindow(void)
  280. {
  281.     tkCloseWindow();
  282.     keyTableCount = 0;
  283.     mouseDownTableCount = 0;
  284.     mouseUpTableCount = 0;
  285.     mouseLocTableCount = 0;
  286. }
  287.  
  288. void auxQuit(void)
  289. {
  290.     tkQuit();
  291. }
  292.  
  293. void auxSwapBuffers(void)
  294. {
  295.     tkSwapBuffers();
  296. }
  297.  
  298. Display *auxXDisplay(void)
  299. {
  300.     Display *ptr;
  301.     
  302.     tkGetSystem(TK_X_DISPLAY, (void *)&ptr);
  303.     return ptr;
  304. }
  305.  
  306. Window auxXWindow(void)
  307. {
  308.     Window ptr;
  309.     
  310.     tkGetSystem(TK_X_WINDOW, (void *)&ptr);
  311.     return ptr;
  312. }
  313.  
  314. int auxXScreen(void)
  315. {
  316.     int screen;
  317.   
  318.     tkGetSystem(TK_X_SCREEN, (void *)&screen);
  319.     return(screen);
  320. }
  321.  
  322. GLenum auxGetDisplayModePolicy(void)
  323. {
  324.     return tkGetDisplayModePolicy();
  325. }
  326.  
  327. GLint auxGetDisplayModeID(void)
  328. {
  329.     return tkGetDisplayModeID();
  330. }
  331.  
  332. GLenum auxGetDisplayMode(void)
  333. {
  334.     return tkGetDisplayMode();
  335. }
  336.  
  337. long auxGetCurrentContext(void)
  338. {
  339.     long id;
  340.     
  341.     tkGetSystem(TK_CONTEXT, (void *)&id);
  342.     return id;
  343. }
  344.  
  345. void auxSetOneColor(int index, float r, float g, float b)
  346. {
  347.     tkSetOneColor(index, r, g, b);
  348. }
  349.  
  350. void auxSetFogRamp(int density, int startIndex)
  351. {
  352.     tkSetFogRamp(density, startIndex);
  353. }
  354.  
  355. void auxSetGreyRamp(void)
  356. {
  357.     tkSetGreyRamp();
  358. }
  359.  
  360. void auxSetRGBMap(int size, float *rgb)
  361. {
  362.     tkSetRGBMap(size, rgb);
  363. }
  364.  
  365. int auxGetColorMapSize(void)
  366. {
  367.  
  368.     return tkGetColorMapSize();;
  369. }
  370.  
  371. void auxGetMouseLoc(int *x, int *y)
  372. {
  373.     tkGetMouseLoc(x, y);
  374. }
  375.